﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DailyLog
{
    internal class TextUtils
    {
        /// <summary>
        /// 从HTML内容中提取纯文本
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public static string GetSummaryFromHtml(string htmlContent)
        {
            string plainText = string.Empty;
            if (!string.IsNullOrEmpty(htmlContent))
            {
                // 移除HTML标签和Base64图片数据
                plainText = System.Text.RegularExpressions.Regex.Replace(htmlContent, @"<[^>]+>|data:image\/[^;]+;base64,[^""]+", "").Trim();
                // 将连续的空白字符替换为单个空格
                plainText = System.Text.RegularExpressions.Regex.Replace(plainText, @"\s+", " ");
            }
            return plainText;
        }

        public static string BuildContentForRows(DataRowCollection rows)
        {
            StringBuilder contentBuilder = new StringBuilder();
            for (int i = 0; i < rows.Count; i++)
            {
                DataRow row = rows[i];
                string logAuthor = row["author"].ToString();
                string date = Convert.ToDateTime(row["logdate"]).ToString("yyyy-MM-dd");
                string content = row["content"].ToString();

                contentBuilder.Append($"<div class=\"log-header\">作者：{logAuthor} | 日期：{date}</div>");
                contentBuilder.Append(content);

                // 如果不是最后一行,添加分隔符
                if (i < rows.Count - 1)
                {
                    contentBuilder.Append("<div class=\"log-divider\"></div>");
                }
            }
            return contentBuilder.ToString();
        }
    }
}
